<?php
	header('content-type:text/html;charset=utf-8');
	date_default_timezone_set("Asia/Chongqing");
	require('../config/config.php');
	require('../class/function.php');
	require('../class/autoload.php');
	session_start();

	//dump($_POST,1);


	//判断是否为微信浏览器
	// if(!CheckMicroBrowser()){
	// 	exit('10005');
	// }


	//判断来路域名是否合法
    if(!CheckOrignDomain()){
    	exit('10001');
    }

	//判断来路路径是否在合法路径集中
	if(!CheckReferer()){
		exit('10002');
	}

	//非登陆请求，进行token1验证,并生成返回新token1
	$user = I('user','p');
	$token1 = I('token1','p');
	if(!$user){
		exit('10003');
	}
	if($_REQUEST['type'] != 'login'){
		//echo $user.'--'.$token1;

		if(!$token1){
			exit('10004');
		}
		$token3 = VerifyToken1($user,$token1);	//验证token 
		if(!$token3){
			//exit('10005');
		}
		$tmp_arr = array(
			'token1'=>$token3
		);
	}

	switch ($_REQUEST['type']) {

		default :
			echo 'default';
			break;


		//修改小区信息状态
		case 'changeMsgStat':
			$msg_id = intval(I('msg_id','p'));
			$area_id = intval(I('area_id','p'));
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			if(!$msg_id || !$area_id || !$db_host || !$db_name){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoCounty::init();
			$sql = 'update area_msg_list set stat=if(stat+1<4,stat+1,1) where id = ? and area_id = ?';
			$res = $pdo->dml($sql,array($msg_id,$area_id));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;


		////获取指定城市的小区信息列表
		case 'getCityInfoList':
			$pnow = intval(I('pnow','p'));
			$city_code = I('city_code','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			if(!$city_code || !$db_host || !$db_name){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$limit = 3;
			$limit_start = ($pnow - 1) * $limit;
			$pdo = MyPdoCounty::init();
			$sql = "select id,area_id,openid,info_title,info_content,time,stat from area_msg_list where city_code = ? limit {$limit_start},{$limit}";
			$res = $pdo->dql($sql,array($city_code));
			if($res){
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['data'] = array();
			}
			exit(json_encode($tmp_arr));
			break;


		//更改管理员密码
		case 'UpdateAdminUserPwd':
			$p1 = I('p1','p');
			$p2 = I('p2','p');
			if(!$p1 || !$p2){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}

			$pdo = MyPdoPublic::init();
			$sql = 'update admin_user set pwd = ? where user = ? and pwd = ?';
			$res = $pdo->dml($sql,array(md5($p2),$user,md5($p1)));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;


		//更改小区用户状态
		case 'ChangeAreaUserLevel':
			$area_id = intval(I('area_id','p'));
			$openid = I('openid','p');
			$city_code = I('city_code','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			if(!$area_id || !$openid || !$city_code || !$db_host || !$db_name){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoCounty::init();
			$sql = "update area_users_list set level = if((level+1)>3,1,(level+1)) where area_id=? and openid = ?";
			$res = $pdo->dml($sql,array($area_id,$openid));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;



		//更改小区用户状态
		case 'ChangeAreaUserStat':
			$area_id = I('area_id','p');
			$openid = I('openid','p');
			$city_code = I('city_code','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			if(!$area_id || !$openid || !$city_code || !$db_host || !$db_name){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}

			$pdo = MyPdoCounty::init();
			$sql = 'update area_users_list set stat = if((stat+1)>3,1,(stat+1)) where area_id=? and openid = ?';
			$res = $pdo->dml($sql,array($area_id,$openid));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;


		//获取小区用户列表
		case 'GetAreaUserList':
			$area_id = intval(I('id','p'));
			$city_code = I('city_code','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			$userType = I('userType','p');
			
			if(!$area_id || !$city_code || !$db_host || !$db_name || !$userType){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}

			$pnow = intval(I('pnow','p'));
			$pnow = $pnow > 0 ? $pnow:1;
			$limit = 6;
			$limit_start = ($pnow-1) * $limit;

			$pdo = MyPdoCounty::init();
			if($userType == 'admin'){	//管理
				$where_str = 'and level < 3';
			}else if($userType == 'normal'){ //普通
				$where_str = 'and level = 3';
			}else{	//全部
				$where_str = '';
			}
			$sql = 'select area_id,openid,level,stat from area_users_list where area_id = ?'.$where_str." limit {$limit_start},{$limit}";
			$res = $pdo->dql($sql,array($area_id));
			if($res){
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['data'] = array();
			}
			exit(json_encode($tmp_arr));
			break;





		//更改小区状态
		case 'ChangeAreaStat':
			$id = intval(I('id','p'));
			$city_code = I('city_code','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			if(!$id || !$city_code || !$db_host || !$db_name){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoCounty::init();
			$sql = 'update area_list set stat = if((stat+1)>3,1,(stat+1)) where id = ? and city_code = ?';
			$res = $pdo->dml($sql,array($id,$city_code));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;


		//修改小区状态
		case 'ChangeAreaStat':

			$id = intval(I('id','p'));
			$city_code = I('city_code','p');
			$db_name = I('db_name','p');
			$db_code = I('db_code','p');
			dump($_POST);
			break;


		//获取当前城市的小区列表
		case 'GetAreaListForCurrentCity':
			$pnow = intval(I('pnow','p'));
			$stat = I('stat','p');
			$city_code = I('city_code','p');
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			if(!$stat || !$city_code || !$db_host || !$db_name){
				$tmp_arr['data'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoCounty::init();

			$limit = 2;
			$limit_start = ($pnow-1)*$limit;

			if($stat == 'all'){
				$sql = "select id,area_name,stat from area_list where city_code = ? order by id desc limit {$limit_start},{$limit}";
				$res = $pdo->dql($sql,array($city_code));
			}else{
				$sql = "select id,area_name,stat from area_list where city_code = ? and stat = ? order by id desc limit {$limit_start},{$limit}";
				$res = $pdo->dql($sql,array($city_code,$stat));
			}
			if($res){
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['data'] = array();
			}
			exit(json_encode($tmp_arr));
			break;



		//### 登陆
		case 'login':
			$pwd = I('pwd','p');
			if(!$user || !$pwd){
				exit('err1');	//缺少参数
			}
			$tmp_arr['token1'] = SetToken1($user);
			if($user == 'admin'){
				if($pwd == 'a'){
					$tmp_arr['stat'] = 'ok';	//登陆成功
				}else{
					$tmp_arr['stat'] = 'err2';	//用户密码不正确
				}
				exit(json_encode($tmp_arr));
			}else{
				$pdo = MyPdoPublic::init();
				$sql = "select pwd,stat from admin_user where user = ?";
				$res = $pdo->dql($sql,array($user),1);
				if($res){
					if($res['pwd'] == md5($pwd)){
						if($res['stat'] != 1){
							$tmp_arr['stat'] = 'err1';	//用户已封禁
						}else{
							$tmp_arr['stat'] = 'ok';	//登陆成功
						}
					}else{
						$tmp_arr['stat'] = 'err2';	//用户密码不正确
					}
				}else{
					$tmp_arr['stat'] = 'err3';	//用户名或密码不正确
				}
				echo json_encode($tmp_arr);

			}
			break;




		//### 公共数据库设置
		case 'public_database_set':
			$db_addr = I('db_addr','p');
			$db_name = I('db_name','p');
			if(!$db_addr || !$db_name){
				$tmp_arr['data'] = 'err';	//缺少必填项
			}else{
				$arr = array(
					$db_name,
					$db_addr
				);
				$tmp_arr['data'] = $arr;
				ToSaveData('/public_database/public.html',json_encode($arr),$config);
			}
			exit(json_encode($tmp_arr));	//缺少必填项
			break;




		//### 删除开通服务器域名
		case 'del_domain':
			$ids = I('domain','p');
			if(!$ids){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$ids = explode(',', $ids);
			$ident = '';
			foreach($ids as $v){
				$arr[] = $v;
				$ident .="?,";
			}
			$ident = trim($ident,',');
			$pdo = MyPdoPublic::init();
			$sql = "delete from open_city_list where id in ($ident)";
			$res = $pdo->dml($sql,$arr);
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;


		//### 添加开通服务器域名
		case 'add_domian':
			$domain = I('domain','p');
			if(!$domain){
				$tmp_arr['stat'] = 'err3';	//缺少参数
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoPublic::init();
			$sql = 'select id from open_city_list where domain = ?';
			$res = $pdo->dql($sql,array($domain));
			if(!$res){
				$sql2 = 'insert into open_city_list values(null,?)';
				$res2 = $pdo->dml($sql2,array($domain));
				if($res2){
					$tmp_arr['stat'] = 'ok';
				}else{
					$tmp_arr['stat'] = 'err2';	//添加失败
				}
			}else{
				$tmp_arr['stat'] = 'err1';	//域名存在
			}
			exit(json_encode($tmp_arr));
			exit;
			break;

		//### 获取开通的域名列表
		case 'get_domain_list':
			//$token = VerifyToken($user,$ip,$token1);
			$pdo = MyPdoPublic::init();
			$sql = 'select id,domain from open_city_list order by id desc';
			$res = $pdo->dql($sql,array());
			if($res){
				foreach($res as $v){
					$list[] = [$v['id'],$v['domain']];
					$list2[] = $v['domain'];
				}
				$tmp_arr['data'] = $list;
				//添加到upyun的数据不能带ip，只供前台使用
				$list3 = json_encode($list2);
			}else{
				$tmp_arr['data'] = $list3 = '[]';
			}
			ToSaveData('/server_list/server_list.html',$list3,$config);
			exit(json_encode($tmp_arr));
			break;









		//### 设置开通城市的库址库名
		case 'set_open_city':
			$db_host = I('db_host','p');
			$db_name = I('db_name','p');
			$city_id = I('city_id','p');
			if(!$city_id){
				exit('err1');
			}
			$pdo = MyPdoPublic::init();
			$sql = 'update city set db_host=?,db_name=? where id=?';
			$res = $pdo->dml($sql,array(IpEncrypt($db_host),$db_name,$city_id));
			if($res){
				$tmp_arr['stat'] = 'ok';
				get_open_city($config);
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;





		//### 获取全部城市信息
		case 'get_city_data':
			$pdo = MyPdoPublic::init();
			$sql = 'select id,code,name,parent_id,level,db_host,db_name from city order by id';
			$res = $pdo->dql($sql,array());
			if($res){
				$data = array();
				foreach($res as $v){
					$v['db_host'] = $v['db_host']?IpDecrypt($v['db_host']):$v['db_host'];
					$arr = array(
						$v['id'],
						$v['parent_id'],
						$v['level'],
						$v['code'],
						$v['name'],
						$v['db_host'],
						$v['db_name']
					);
					$data[] = $arr;
				}
				$tmp_arr['data'] = $data; 
			}else{
				$tmp_arr['data'] = $data;
			}
			exit(json_encode($tmp_arr));
			break;










		//获取管理员的对县的权限列表
		case 'get_admin_user_perm_list':
			$user_id = I('user_id','p');
			if(!$user_id){
				$tmp_arr['data']='err';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoPublic::init();
			$sql = 'select perm from admin_user where id = ?';
			$res = $pdo->dql($sql,array($user_id),1);
			if($res && strlen($res['perm']) > 0){
				$tmp_arr['data'] = $res['perm'];
			}else{
				$tmp_arr['data'] = '[]';
			}
			exit(json_encode($tmp_arr));
			break;


		//设置管理员的县管理权限
		case 'set_admin_perm':
			$city_id_list = I('city_id_list','p');
			$user_id = I('user_id','p');
			if(!$city_id_list || !$user_id){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}

			$pdo = MyPdoPublic::init();
			$sql = 'update admin_user set perm = ? where id = ?';
			$res = $pdo->dml($sql,array($city_id_list,$user_id));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;



		//### 更改管理成员状态 
		case 'change_admin_user_stat':
			$id = I('id','p');
			if(!$id){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoPublic::init();
			$sql = 'update admin_user set stat=if((stat+1)>2,1,2) where id = ?';
			$res = $pdo->dml($sql,array($id));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;


		//重置密码
		case 'reset_pwd':
			$id = I('id','p');
			if(!$id){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoPublic::init();
			$sql = 'update admin_user set pwd = ? where id = ?';
			$res = $pdo->dml($sql,array(md5('123'),$id));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;



		//### 删除管理成员
		case 'del_admin_user':
			$id = I('id','p');
			if(!$id){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoPublic::init();
			$sql = 'delete from admin_user where id = ?';
			$res = $pdo->dml($sql,array($id));
			if($res){
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;


		//### 获取管理成员列表
		case 'get_admin_user_list':
			$pdo = MyPdoPublic::init();
			$pnow = I('pnow','p');
			$psize = I('psize','p');
			$search = I('search','p');
			$start = ($pnow-1) * $psize;
			if($search){
				$sql = "select id,user,stat from admin_user where id > 1 and user!='admin' and user like ? order by id desc limit {$start},{$psize}";
				$arr_p = array("%{$search}%");
			}else{
				$sql = "select id,user,stat from admin_user where id > 1 and user!='admin' order by id desc limit {$start},{$psize}";
				$arr_p = array();
			}
			$res = $pdo->dql($sql,$arr_p);
			if($res){
				$tmp_arr['data'] = $res;
			}else{
				$tmp_arr['data'] = array();
			}
			echo json_encode($tmp_arr);
			break;


		//### 添加后台管理成员
		case 'add_admin_user':
			$user_name = I('user_name','p');
			if(!$user_name){
				$tmp_arr['stat'] = 'err1';
				exit(json_encode($tmp_arr));
			}
			$pdo = MyPdoPublic::init();
			$sql = 'select id from admin_user where user = ?';
			$res = $pdo->dql($sql,array($user_name));
			if(!$res){
				$sql2 = 'insert into admin_user values(null,?,md5("123"),"",1)';
				$res2 = $pdo->dml($sql2,array($user_name));
				$tmp_arr['stat'] = 'ok';
			}else{
				$tmp_arr['stat'] = 'err2';
			}
			exit(json_encode($tmp_arr));
			break;


		//获取开通城市信息
		case 'get_open_city_list':
			echo $city_list = get_open_city($config);
			break;


		case 'get_open_city':
			//限制短时频繁请求
			$shortLimitTime = 1;
			$recoverfile = '../cache/tmp/openCity_time_limit';
			if(S($recoverfile)){
				$t1 = intval(S($recoverfile));
				if($t1 > time()){
					exit;
				}else{
					S($recoverfile,time()+$shortLimitTime);
				}
			}else{
				S($recoverfile,time()+$shortLimitTime);
			}
			$res = get_open_city($config);
			if($res){
				$tmp_arr['data'] = $res;
				exit(json_encode($tmp_arr));
			}
			break;




		
	}







/*
	### 使用用户名和ip组合成token并返回
*/
// function SetToken($user,$ip){
// 	$token = md5($user.$ip.ceil(time()/3*2));
// 	S('token/'.$user,$token);
// 	return $token;
// }


/*
	### 验证token 
	示例：
		$token = VerifyToken($user,$ip,$token1);
*/
// function VerifyToken($user,$ip,$token1){
// 	$token2 = S('token/'.$user);
// 	//echo $token2.' == '.$token1;
// 	if($token2 == $token1){
// 		return SetToken($user,$ip);
// 	}else{
// 		return false;
// 	}
// }


